<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理" lazy>
          <el-button
            type="primary"
            style="margin-top: 15px"
            @click="addClick"
          >新增角色</el-button>
          <el-table :data="list">
            <el-table-column label="序号">
              <template v-slot="{ $index }">
                <div>
                  <!-- （页码 - 1）* 页容量 + 下标（序号）+ 1  -->
                  {{ (page.page - 1) * page.pagesize + $index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="角色名" prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <!-- <template v-slot="{$index,row}"> -->
              <template v-slot="{ row }">
                <div>
                  <el-button
                    type="text"
                    @click="setClick(row.id)"
                  >分配权限</el-button>
                  <el-button type="text" @click="edit(row)">修改</el-button>
                  <el-button type="text" @click="del(row.id)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div class="page">
            <el-pagination
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              :current-page="page.page"
              :page-sizes="[1, 5, 10, 100, 200]"
              :page-size="page.pagesize"
              @size-change="sizeChange"
              @current-change="pageChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息" lazy> <Info /> </el-tab-pane>
      </el-tabs>
    </el-card>
    <Add ref="add" :show.sync="show" @getData="getData" />
    <!-- <Add ref="add" /> -->
    <PermissionSet ref="permissionSet" />
  </div>
</template>
<script>
import Add from './components/add.vue'
import { sysRole, sysRoleDelete } from '@/api/setting'
import Info from './components/info.vue'
import PermissionSet from './components/permissionSet.vue'
export default {
  components: {
    Info,
    Add,
    PermissionSet
  },
  data() {
    return {
      show: false,
      list: [],
      page: {
        total: 100,
        page: 1,
        pagesize: 5
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 页容量改变
    sizeChange(size) {
      this.page.pagesize = size
      // 页容量改变时最好都回到第一页
      this.page.page = 1
      this.getData()
    },
    // 页码改变
    pageChange(page) {
      this.page.page = page
      this.getData()
    },
    async getData() {
      const res = await sysRole(this.page)
      this.list = res.data.rows
      this.page.total = res.data.total
    },
    // 点击新增
    addClick() {
      // this.$refs.add.isShow = true
      this.show = true
      this.$refs.add.mode = 'add'
    },
    // 点击删除
    del(id) {
      this.$confirm('您确定删除该角色吗?', '提示')
        .then(async() => {
          await sysRoleDelete(id)
          this.$message.success('删除成功')
          this.page.page = 1
          this.getData()
        })
        .catch(() => {})
    },
    edit(row) {
      this.show = true
      this.$refs.add.mode = 'edit'
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
    },
    setClick(id) {
      this.$refs.permissionSet.isShow = true
      this.$refs.permissionSet.getRoleInfo(id)
    }
  }
}
</script>

<style lang="scss" scoped>
.setting {
  padding: 15px;
  .page {
    text-align: right;
    margin-top: 20px;
  }
}
</style>
